<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/19
 * Time: 16:51

 * 订单类
 */

namespace app\console\controller;
use app\console\model\ServiceOrderGoods;
use think\Build;
use think\Controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use app\interfaces\controller\Alipay;
use think\Exception;
use app\console\model\RefundOrderAudit;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Cache;
class GiftOrder extends Controller
{
    /**
     * @return array|mixed|\think\response\View
     * 订单列表
     */
    public function index()
    {
        $param = Request::instance()->param();
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
        $where['o.order_type'] = ['eq', 3];
        $where['o.status'] = ['in','1,6,5,4,10,11,12,20'];
        $auding_status = Session::get('auding_status');
        if($auding_status == 1){
           // $where['shop_id'] = ['eq',Db::name('shop_create')->where('manage_id',Session::get('manage_id'))->value('shop_id')];
        }
        if (!empty($param['status'])){
            if($param['status'] == 2){
                $where['o.status'] = ['in', $this->status];
            }else{
                $where['o.status'] = ['eq', $param['status']];
            }
        }
        if (!empty($param['is_certification'])) $where['m.is_certification'] = ['eq', $param['is_certification']];
        if (!empty($param['order_type'])) $where['o.order_type'] = ['eq', $param['order_type']];
        if (!empty($param['shop_id'])) $where['shop_id'] = ['eq', $param['shop_id']];
        if (!empty($param['title'])) $where['o.username|o.shop_order_number|o.order_number|m.car_number'] = ['like', '%'.$param['title'].'%'];
        if (!empty($param['interval_timer'])) {
            $interval_timer = explode('/', $param['interval_timer']);
            $where['o.create_time'] = ['between', [$interval_timer[0], $interval_timer[1]]];
        }
        $data = \app\console\model\Order::alias('o')
            ->join('member m', 'o.member_id = m.id','left')
            ->join('shop s', 'o.shop_id = s.id','left')
            ->field('m.username,m.car_number,m.is_certification,o.*,s.shop_title')
            ->where($where)
            ->order('id','desc')
            ->paginate(10, false, ['query' => $param])->each(function($item,$key) use ($level){
                $item->message = 1;//默认没有未读消息
                $gift_news = Db::name('gift_news')->where('order_id', $item->id)->where('manage_id',session('manage_id'))->find();
                if(empty($gift_news)){ //包含有未读消息
                    $item->message = 2;
                }
                if(session('role_id') != 1){
                    $item->username = hideStr($item->username,1,6);
                }
                if($item['status'] == 12 || $item['status'] == 10 || $item['status'] == 11){
                    $item->refund_create_time = Db::name('order_refund')->where('order_id',$item['id'])->value('create_time');
                }
                if($item->is_certification == 2 && !empty($item->car_number)){
                    //$item->car_number = '('.$item->car_number.')';
                }
                if($item->t_type == 2){
                    $item->gift_title = '(会员卡)'.$item->mc_title;
                }
                $item->refund_button = false;
                $count = (new RefundOrderAudit())->where('order_id',$item->id)->where('status','11')->count();
                if($level == 7){
                    if($count == 0){
                        $item->refund_button = true;
                    }
                }
                if($level == 8){
                    if($count == 1){
                        $item->refund_button = true;
                    }
                }
                if($level == 9){
                    if($count == 2){
                        $item->refund_button = true;
                    }
                }
                if($level == 10){
                    if($count == 3){
                        $item->refund_button = true;
                    }
                }
            });
        $total_count = Db::name('order o')
            ->join('member m', 'o.member_id = m.id')
            ->join('shop s', 'o.shop_id = s.id','left')
            ->field('o.*,m.username,s.shop_title')
            ->where($where)->count();
        $total_price = Db::name('order o')
            ->join('member m', 'o.member_id = m.id')
            ->join('shop s', 'o.shop_id = s.id','left')
            ->field('o.*,m.username,s.shop_title')
            ->where($where)->sum('amount');
        $shop_list = Db::name('shop s')->field('id,shop_title')->select();

        return view('', ['data' => $data,
            'shop_list' => $shop_list,
            'total_count' => $total_count,
            'total_price' => $total_price,
        ]);
    }

    public function delete($id)
    {
        $ret = Db::name('shop_assignor_records')->where('id', $id)->delete();
        if ($ret) {
            return ['code' => 200, 'message' => '删除成功'];
        }
        return ['code' => 400, 'message' => '失败'];
    }


    public function iii(){
        $j = 0;
        ++$j;
        return $j;
    }

    /**
     * @param $id
     * 订单修改
     * 6已完成
     */
    public function update($id)
    {
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
        $param = Request::instance()->param();
        $find = Db::name('order')->find($id);
        $auding_status = Session::get('auding_status');
        if($auding_status == 2){
            Db::name('order o')->where('id',$id)->setField('is_read',1);
        }else{
            Db::name('order o')->where('id',$id)->setField('is_read_shop',1);
        }
        //礼包消息读取
        $gift_news = Db::name('gift_news')->where('order_id', $id)->where('manage_id',session('manage_id'))->find();
        if(empty($gift_news)){ //如果没有就添加 如果有不处理
            Db::name('gift_news')->insert(['order_id' => $id,'manage_id' => session('manage_id')]);
        }
        $refund = Db::name('order_refund ors')->field('ors.*,rr.title')->where('order_id',$find['id'])->join('refund_reason rr','ors.reason_id = rr.id')->find();
        if (Request::instance()->isPost()) {
            $status = Db::name('order')->where('id',$id)->value('status');
            $gift_end_time = Db::name('order')->where('id',$id)->value('gift_end_time');
            if(($gift_end_time) < date('Y-m-d H:i:s',time())){
                //fail('礼包已经过期,不能再添加服务');
            }
            if ($status == 6) {
                $shop_assignor_records = [];
                if(empty($param['assignor_id'])){
                    fail('请添加门店人员');
                }
                if(empty($param['shop_id'])){
                  //  fail('门店人员必须添加');
                }
                if (!empty($param['assignor_id']) && count($param['assignor_id']) != count(array_unique($param['assignor_id']))) {
                    //fail('服务人员不能重复');
                }
                if(count(array_unique($param['single_id'])) >= 2){
                    //fail('一次只能服务一个分类');
                }
                if(!empty($param['spec_id']) && !empty($param['number'])){ //服务商品
                    for ($i=0;$i<count($param['spec_id']);$i++){
                        $auding_status = Session::get('auding_status');
                        if($auding_status == 1){
                            $shop_id =  \session('shop_id');
                        }else{
                            $shop_id = Request::instance()->param()['shop_id'][$i];
                        }

                        (new ServiceOrderGoods())->save([
                            'shop_id' => $shop_id,
                            'order_id' => $find['id'],
                            'cat_id' => $param['cat_id'][$i],
                            'spec_id' => $param['spec_id'][$i],
                            'number' => $param['number'][$i],
                            'create_time' => date('Y-m-d H:i:s',time())
                        ]);
                        $inventory = Db::name('shop_spec_attr')->where('id',$param['spec_id'][$i])->setDec('inventory',$param['number'][$i]);
//                                    $service_order_goods['order_id'] = $service_order['id'];
//                                    $service_order_goods['cat_id'] = $service_order['cat_id'];
//                                    $service_order_goods['spec_id'] = $service_order['spec_id'];
//                                    $service_order_goods['number'] = $service_order['number'];
//                                    $service_order_goods['create_time'] = $service_order['create_time'];
                    }
                    //(new ServiceOrderGoods())->saveAll($service_order_goods);
                }
                if(!empty($param['assignor_id'])){
                    //$single_id = explode('-',$param['single_id'][0])[0];//门店服务分类 第一个类就代表所有相同的服务类了
                    $single_title = Db::name('single')->where('id',$param['single_id'][0])->value('cat_name');
                    //$assignor_price = explode('-',$param['single_id'][0])[1];//门店金额
                    for ($i = 0; $i < count($param['assignor_id']); $i++) {
                        $auding_status = Session::get('auding_status');
                        if($auding_status == 1){
                            $shop_id =  \session('shop_id');
                        }else{
                            $shop_id = Request::instance()->param()['shop_id'][$i];
                        }
//                        $shop_assignor_records[$i]['order_id'] = $find['id'];
//                        $shop_assignor_records[$i]['shop_id'] = $shop_id;
//                        $shop_assignor_records[$i]['assignor_id'] = $param['assignor_id'][$i];
//                        $shop_assignor_records[$i]['single_id'] = $single_id;
//                        $shop_assignor_records[$i]['assignor_price'] = $assignor_price;
//                        $shop_assignor_records[$i]['assignor_content'] = $param['assignor_content'][$i];
//                        $shop_assignor_records[$i]['create_time'] = date('Y-m-d H:i:s', time());
//                        $shop_assignor_records[$i]['order_type'] = $find['order_type'];

                        $shop_assignor_records_id = Db::name('shop_assignor_records')->insertGetId(
                            ['assignor_id' => $param['assignor_id'][$i],
                                'assignor_price' => $param['assignor_price'][$i],
                                'single_id' => $param['single_id'][$i],
                                'order_id' => $find['id'],
                                'order_type' => $find['order_type'],
                                'assignor_content' => $param['assignor_content'][$i],
                                'create_time' => date('Y-m-d H:i:s',time()),
                                'shop_id' => $shop_id
                            ]);

                        Db::name('member_message')->insert(
                            ['member_id' => $find['member_id'],
                                'order_number' => $shop_assignor_records_id,
                                'type_id' => 4,
                                'title' => $find['gift_title'].'礼包已设置,请确认使用',
                                'content' => '服务类型:'.$single_title,
                                'is_click' => 1,
                                'create_time' => date('Y-m-d H:i:s', time())
                            ]);

                    }
                    //Db::name('order_gift_count')->where('order_id',$id)->where('single_id',$single_id)->setDec('gift_count');//礼包剩余次数减少
                    (new \app\index\controller\MemberMessage())->jpushMessage($find['member_id'], $find['gift_title'].'礼包已设置,请确认使用','服务类型:'.$single_title, 4);

                    //Db::name('shop_assignor_records')->insertAll($shop_assignor_records);
                }
                   Db::name('order')->where('id',$id)->where('status',6)->update(['status' => 5]);
                    win('门店人员分配完毕', Url::build('index'));

            }
            if ($status == 5 || $status == 4) {
                $shop_id = Db::name('shop_create')->where('manage_id',Session::get('manage_id'))->value('shop_id');//当前创建的门店id;
                //if(empty($shop_id)) fail('当前管理员不是门店管理员');
                if(empty($param['assignor_id'])){
                    fail('请添加门店人员');
                }
                if (!empty($param['assignor_id']) && count($param['assignor_id']) != count(array_unique($param['assignor_id']))) {
                    //fail('服务人员不能重复');
                }
                if(isset($param['single_id']) && count(array_unique($param['single_id'])) >= 2){
                    //fail('一次只能服务一个分类');
                }
                if(isset($param['shop_id']) &&  count(array_unique($param['shop_id'])) >= 2){
                    //fail('一次只能服务一个门店');
                }
                if(!empty($param['spec_id']) && !empty($param['number'])){ //服务商品
                    $service_order_goods = [];
                    for ($i=0;$i<count($param['spec_id']);$i++){
                        $auding_status = Session::get('auding_status');
                        if($auding_status == 1){
                            $shop_id =  \session('shop_id');
                        }else{
                            $shop_id = Request::instance()->param()['shop_id'][$i];
                        }
                                        //服务商品
                        (new ServiceOrderGoods())->save([
                            'shop_id' => $shop_id,
                            'order_id' => $find['id'],
                            'cat_id' => $param['cat_id'][$i],
                            'spec_id' => $param['spec_id'][$i],
                            'number' => $param['number'][$i],
                            'create_time' => date('Y-m-d H:i:s',time())
                        ]);
                        $inventory = Db::name('shop_spec_attr')->where('id',$param['spec_id'][$i])->setDec('inventory',$param['number'][$i]);
//                                    $service_order_goods['order_id'] = $service_order['id'];
//                                    $service_order_goods['cat_id'] = $service_order['cat_id'];
//                                    $service_order_goods['spec_id'] = $service_order['spec_id'];
//                                    $service_order_goods['number'] = $service_order['number'];
//                                    $service_order_goods['create_time'] = $service_order['create_time'];
                    }
                    //(new ServiceOrderGoods())->saveAll($service_order_goods);
                }
                if(!empty($param['assignor_id'])){
                    //$single_id = explode('-',$param['single_id'][0])[0];//门店服务分类 第一个类就代表所有相同的服务类了

                    //$assignor_price = explode('-',$param['single_id'][0])[1];//门店金额
                    $single_title = '';
                    for ($i = 0; $i < count($param['assignor_id']); $i++) {

                        $single_array[] = Db::name('single')->where('id',$param['assignor_content'][$i])->value('cat_name');
                        $auding_status = Session::get('auding_status');
                        if($auding_status == 1){
                            $shop_id =  \session('shop_id');
                        }else{
                            $shop_id = Request::instance()->param()['shop_id'][$i];
                        }
//                        $shop_assignor_records[$i]['order_id'] = $find['id'];
//                        $shop_assignor_records[$i]['shop_id'] = $shop_id;
//                        $shop_assignor_records[$i]['assignor_id'] = $param['assignor_id'][$i];
//                        $shop_assignor_records[$i]['single_id'] = $single_id;
//                        $shop_assignor_records[$i]['assignor_price'] = $assignor_price;
//                        $shop_assignor_records[$i]['assignor_content'] = $param['assignor_content'][$i];
//                        $shop_assignor_records[$i]['create_time'] = date('Y-m-d H:i:s', time());
//                        $shop_assignor_records[$i]['order_type'] = $find['order_type'];

                        $shop_assignor_records_id = Db::name('shop_assignor_records')->insertGetId(
                            ['assignor_id' => $param['assignor_id'][$i],
                                'assignor_price' => $param['assignor_price'][$i],
                                'single_id' => $param['single_id'][$i],
                                'order_id' => $find['id'],
                                'order_type' => $find['order_type'],
                                'assignor_content' => $param['assignor_content'][$i],
                                'create_time' => date('Y-m-d H:i:s',time()),
                                'shop_id' => $shop_id
                            ]);



                    }
                    $single_array = array_unique($single_array);//去重
                    $single_title = implode(',',$single_array);//打成字符串
                    Db::name('member_message')->insert(
                        ['member_id' => $find['member_id'],
                            'order_number' => $shop_assignor_records_id,
                            'type_id' => 4,
                            'title' => $find['gift_title'].'礼包已设置,请确认使用',
                            'content' => '服务内容:'.$single_title,
                            'is_click' => 1,
                            'create_time' => date('Y-m-d H:i:s', time())
                        ]);
                    //Db::name('order_gift_count')->where('order_id',$id)->where('single_id',$single_id)->setDec('gift_count');//礼包剩余次数减少

                    //Db::name('shop_assignor_records')->insertAll($shop_assignor_records);
                    (new \app\index\controller\MemberMessage())->jpushMessage($find['member_id'], $find['gift_title'].'礼包已设置,请确认使用','服务内容:'.$single_title, 4);
                }

                win('门店人员分配完毕.', Url::build('index'));
            }

            if (isset($param['status']) && ($param['status'] >= 10)) {

                $fail_explain = Request::instance()->param('fail_explain','');
                if(strpos('5,7,8,9,10',(string)$level) === false){
                    fail('当前管理员无退款审核权限');
                }
                $refund_order_audit = Db::name('refund_order_audit')->where([
                    'order_id' => $find['id'],
                    'manage_id' => \session('manage_id'),
                ])->find();//当前管理员自己提交的数据
                if(!empty($refund_order_audit)) fail('已经申请过审核');
                if($level == 5){ //门店点击提交 店长-副经理-经理-财务-总经理
                    if(empty($refund_order_audit)){
                        (new RefundOrderAudit())->save([
                            'order_id' => $find['id'],
                            'manage_id' => \session('manage_id'),
                            'status' => $param['status'],
                            'is_read' => 1,
                            'level' => $level,
                            'shop_id' => \session('shop_id'),
                            'shop_title' => Db::name('shop')->where('id',\session('shop_id'))->value('shop_title'),
                            'fail_explain' => $fail_explain,
                            'create_time' => date('Y-m-d H:i:s',time())
                        ]);
                        Db::name('order')->where('id',$find['id'])->setField('is_read_5',1);
                        if($param['status'] == 12){
                            Db::name('order')->where('id',$find['id'])->setField('status',12);
                        }
                        win('门店申请退款审核',Url::build('index'));
                    }else{
                        fail('已经申请审核');
                    }
                }
                $refund_order_audit_5 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>5])->find();//门店是否提交审核数据
                $refund_order_audit_7 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>7])->find();//门店是否提交审核数据
                $refund_order_audit_8 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>8])->find();//门店是否提交审核数据
                $refund_order_audit_9 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>9])->find();//门店是否提交审核数据
                if($level == 7){ //副经理
                    //if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    $ret = (new RefundOrderAudit())->save([
                        'order_id' => $find['id'],
                        'manage_id' => \session('manage_id'),
                        'status' => $param['status'],
                        'is_read' => 1,
                        'level' => $level,
                        'shop_id' => 0,
                        'fail_explain' => $fail_explain,
                        'create_time' => date('Y-m-d H:i:s',time())
                    ]);
                    Db::name('order')->where('id',$find['id'])->setField('is_read_7',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($ret){
                        win('副经理申请退款审核',Url::build('index'));
                    }

                }
                if($level == 8){ //经理

                    //if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');
                    $ret = (new RefundOrderAudit())->save([
                        'order_id' => $find['id'],
                        'manage_id' => \session('manage_id'),
                        'status' => $param['status'],
                        'is_read' => 1,
                        'level' => $level,
                        'shop_id' => 0,
                        'fail_explain' => $fail_explain,
                        'create_time' => date('Y-m-d H:i:s',time())
                    ]);
                    Db::name('order')->where('id',$find['id'])->setField('is_read_8',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($ret){
                        win('经理申请退款审核',Url::build('index'));
                    }
                }
                if($level == 9){ //财务

                    //if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');
                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');
                    $ret = (new RefundOrderAudit())->save([
                        'order_id' => $find['id'],
                        'manage_id' => \session('manage_id'),
                        'status' => $param['status'],
                        'is_read' => 1,
                        'level' => $level,
                        'shop_id' => 0,
                        'fail_explain' => $fail_explain,
                        'create_time' => date('Y-m-d H:i:s',time())
                    ]);
                    Db::name('order')->where('id',$find['id'])->setField('is_read_9',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($ret){
                        win('财务申请退款审核',Url::build('index'));
                    }
                }
                if($level == 10){ //总经理

                    //if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');
                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');
                    if(empty($refund_order_audit_9)) fail('财务没有通过退款审核');
                    Db::name('order')->where('id',$find['id'])->setField('is_read_10',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($param['status'] == 11){
                        $find = Db::name('order')->find($id);
                        if ($find['pay_type'] == '2') {
                            $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '', $find['shop_order_number']);
                            if ($ret->code !== '10000') {
                                fail($ret->sub_msg);
                            }
                            //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);
                        } else if ($find['pay_type'] == '1') {
                            //halt('wx');
                            $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);
                            if ($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);
                        }
                        $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);
                        if ($ret !== false) {
                            (new Order())->refund($find['member_id'], $find['order_number']);
                            $refund_ret = Db::name('order_refund')->where('order_id', $id)->update(['fail_explain' => $param['fail_explain']]);
                            if ($refund_ret !== false) {
                                win('退货款处理完毕', Url::build('index'));
                            }
                        }
                        $ret = (new RefundOrderAudit())->save([
                            'order_id' => $find['id'],
                            'manage_id' => \session('manage_id'),
                            'status' => $param['status'],
                            'is_read' => 1,
                            'level' => $level,
                            'shop_id' => 0,
                            'fail_explain' => $fail_explain,
                            'create_time' => date('Y-m-d H:i:s',time())
                        ]);
                        Db::name('member_card')->where('order_id',$find['id'])->delete();
                        if($ret){
                            win('总经理同意退款中',Url::build('index'));
                        }
                    }

                }





            } //退款逻辑end
//            if (isset($param['status']) && ($param['status'] == 11 || $param['status'] == 12)) {
//
//                if ($find['pay_type'] == '2') {
//                    $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '',$find['shop_order_number']);
//
//                    if($ret->code !== '10000'){
//                        fail($ret->sub_msg);
//                    }
//                    //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);
//                } else if ($find['pay_type'] == '1') {
//                    //halt('wx');
//
//                    $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);
//                    if($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);
//
//                }
//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);
//                if ($ret !== false) {
//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);
//                    if($refund_ret !== false){
//                        win('退款处理完毕', Url::build('index'));
//                    }
//                }
//            }
//            if (isset($param['status']) && ($param['status'] == 11)) {
//                $find = Db::name('order')->find($id);
//                if ($find['pay_type'] == '2') {
//                    $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '',$find['shop_order_number']);
//                    if($ret->code !== '10000'){
//                        fail($ret->sub_msg);
//                    }
//                    //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);
//                } else if ($find['pay_type'] == '1') {
//                    //halt('wx');
//                    $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);
//                    if($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);
//                }
//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);
//                if ($ret !== false) {
//                    (new Order())->refund($find['member_id'],$find['order_number']);
//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);
//                    if($refund_ret !== false){
//                        win('退货款处理完毕', Url::build('index'));
//                    }
//                }
//            }
//
//            if (isset($param['status']) && ($param['status'] == 12)) {//退款中变成 进行中
//              $ret = Db::name('order')->where('id', $param['id'])->update(['status' => 6]);
//                if ($ret !== false) {
//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);
//                    if($refund_ret !== false){
//                        win('退货款失败处理完毕', Url::build('index'));
//                    }
//                }
//            }
        }

        $info = Db::name('order o')
            ->join('member m', 'o.member_id = m.id','left')
            ->join('shop s', 'o.shop_id = s.id','left')
            ->field('o.*,m.username,m.car_number,m.is_certification,m.brand_number,m.id_code,s.shop_title')
            ->where('o.id', $param['id'])
            ->find();
        if($info['status'] == 10 || $info['status'] == 11 || $info['status'] == 12){
            $info['refund_create_time'] = Db::name('order_refund')->where('order_id',$info['id'])->value('create_time');
        }
        $shop_list = Db::name('shop')->where('is_delete',1)->where('auding_status',2)->select();
        $gift_records = Db::name('gift_assignor_records gr')->join('shop s','gr.shop_id = s.id','left')->field('gr.*,s.shop_title')->where('order_id',$id)->select();
        //$shop_id = Db::name('shop_create')->where('manage_id',Session::get('manage_id'))->value('shop_id');//当前创建的门店id;
        if(\session('auding_status') == 1){
            $shop_id = \session('shop_id');
        }else{
            $shop_id = $shop_list[0]['id'];
        }
        $shop_assignor = Db::name('shop_assignor')->where('shop_id',$shop_id)->select();


        $shop_assignor_records = Db::name('shop_assignor_records sar')
            ->join('shop','shop.id = sar.shop_id','left')
            ->join('shop_assignor sa','sar.assignor_id = sa.id','left')
            ->join('single s','sar.single_id = s.id')
            ->where('order_id', $info['id'])
            ->order('sar.id','desc')
            ->field('sar.*,shop.shop_title,sa.name,s.cat_name')
            ->select()->toArray();
        if(!empty($shop_assignor_records)){
            foreach ($shop_assignor_records as $k => &$v){
                $v['assignor_content'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name');
            }
        }
        $service_order_goods = Db::name('service_order_goods soo')
            ->join('shop','shop.id = soo.shop_id','left')
            ->join('shop_spec_attr ssa','ssa.id = soo.spec_id','left')
            ->join('shop_goods sg','sg.id = ssa.goods_id','left')
            ->where('order_id', $info['id'])
            ->field('soo.*,shop.shop_title,ssa.attribute,sg.title')
            ->select();


        $refund_order_audit_records = (new RefundOrderAudit())
            ->alias('roa')
            ->join('manager m','roa.manage_id = m.id')
            ->field('roa.*,m.username')
            ->where('order_id',$find['id'])
            ->select();
        //审核会员数量 不管通过不通过;
        $count = (new RefundOrderAudit())->where('order_id',$find['id'])->where('status','11')->count();
        $refund_button = false; //这么判断 其他人员肯定是false 就没有退款按钮
//        if($level == 5){
//            if($count == 0){
//                $refund_button = true;
//            }
//        }
        if($level == 7){
            if($count == 0){
                $refund_button = true;
            }
        }
        if($level == 8){
            if($count == 1){
                $refund_button = true;
            }
        }
        if($level == 9){
            if($count == 2){
                $refund_button = true;
            }
        }
        if($level == 10){
            if($count == 3){
                $refund_button = true;
            }
        }
        if(\session('auding_status') == 2){
            $where = [];
        }else{
            $where['shop_id'] = ['eq',\session('shop_id')];
        }

        $spec_attr_list = (new \app\console\model\ShopGoods())
            ->alias('sg')
            ->join('shop_spec_attr ssa','sg.id = ssa.goods_id','left')
            ->join('shop s','s.id = ssa.shop_id')
            ->where($where)
            ->where('ssa.shop_id','gt',0)
            ->where('s.auding_status',2)
            ->where('s.is_delete',1)
            ->field('sg.title,ssa.*,s.shop_title')
            ->paginate(1000, false, ['query' => $param])->each(function ($item, $key)  use ($where){

            });
        //原订单礼包订单商品 //记录
        $order_gift_count = Db::name('order_gift_count')
            ->alias('ogc')
            ->join('single s','ogc.single_id = s.id')
            ->where('order_id',$param['id'])
            ->select()->toArray();
        if(!empty($order_gift_count)){
            foreach ($order_gift_count as $k => &$v){
                $v['cat_name2'] = Db::name('single')->where('id',$v['single_content'])->value('cat_name');
            }
        }
        $now_gift_count = Db::name('order_gift_count')
            ->alias('ogc')
            ->join('single s','ogc.single_id = s.id')
            ->where('order_id',$param['id'])
            ->where('ogc.gift_count','gt',0)
            ->group('ogc.single_id')
            ->select()->toArray();//剩余次数大于0的礼包记录数 默认礼包分类
        if(empty($now_gift_count)){
            $now_gift_count[0]['single_id'] = '';
        }
        $single_id_array = Db::name('order_gift_count')
            ->alias('ogc')
            ->join('single s','ogc.single_content = s.id')
            ->where('order_id',$param['id'])
            ->where('ogc.single_id',$now_gift_count[0]['single_id'])
            ->where('ogc.gift_count','gt',0)
            ->column('ogc.single_content');//剩余次数大于0的礼包记录数 默认礼包分类
        if(!empty($now_gift_count)){
            foreach ($now_gift_count as $k => &$v){ //默认第一个门店的服务类人员金额
                $person_price = Db::name('shop_service_price')->where('shop_id',$shop_id)->where('single_id',$v['single_id'])->value('person_price');
                if(is_null($person_price)) $person_price = '0.00';
               $v['person_price'] =  $person_price; //好像没啥用
                //$single_id_array[] = $v['single_content'];

            }
        }

        if(session('role_id') != 1){
            $info['username'] = hideStr($info['username'],1,6);
        }

        if(!empty($now_gift_count)){
            $service_people_price2 = Db::name('shop_service_price')
                ->alias('ssp')
                ->join('single s','ssp.single_id = s.id','right')
                ->where('shop_id',$shop_id)
                ->where('s.id','in',$single_id_array)
                ->where('auding_status',2)
                ->where('is_delete',1)
                ->field('s.id,s.cat_name,ssp.person_price')
                ->select(); //服务人员提成;
            $service_people_price3 = Db::name('shop_service_price')
                ->alias('ssp')
                ->join('single s','ssp.single_id = s.id','right')
                ->where('shop_id',$shop_id)
                ->where('s.parent_id','eq',$now_gift_count[0]['single_id'])
                ->where('auding_status',2)
                ->where('is_delete',1)
                ->field('s.id,s.cat_name,ssp.person_price')
                ->select(); //服务人员提成;
        }else{
            halt('else');
            $service_people_price2 = $service_people_price3 = '';
        }
        if($info['t_type'] == 1){
            if($service_people_price2->isEmpty()){
                fail('联系总台设置人员提成');
            }
        }

        $template = '';
        if(isset($param['table'])) $template = 'service_order_table';
        $info['cat_name'] = $info['gift_title'];
        $info['one_cat'] = '';

        return view($template, ['shop_list' => $shop_list,
            'item' => $info,
            'order_gift_count' => $order_gift_count,
            'now_gift_count' => json_encode($now_gift_count),
            'service_people_price2' => json_encode($service_people_price2),
            'service_people_price3' => json_encode($service_people_price3),
            'refund' => $refund,
            'shop_assignor' => $shop_assignor,
            'gift_assignor_records' => $shop_assignor_records,
            'shop_assignor_records' => $shop_assignor_records,
            'service_order_goods' => $service_order_goods,
            'refund_order_audit_records' => $refund_order_audit_records,
            'refund_button' => $refund_button,
            'spec_attr_list' => json_encode($spec_attr_list->toArray()['data']),
            'category' => json_encode(ClassifyAlias::dimension(Cache::get('goodsType'))),
        ]);
    }

    public function update_m(){

    }

    public function shop_assignor($shop_id){
        $shop_assignor = Db::name('shop_assignor')->where('shop_id',$shop_id)->select();
        return $shop_assignor;
    }

    /**
     * @param $order_id
     * @param $shop_id
     * @return string
     * ajax当前订单礼包 符合条件的服务类 以及服务金额
     */
    public function now_gift_count($order_id,$shop_id){
        //使用订单id查询当前订单的纪录礼包分类 然后根据门店获得价格
        $now_gift_count = Db::name('order_gift_count')
            ->alias('ogc')
            ->join('single s','ogc.single_id = s.id')
            ->where('order_id',$order_id)
            ->where('ogc.gift_count','gt',0)
            ->group('ogc.single_id')
            ->select()->toArray();//剩余次数大于0的礼包记录数 //一级类
//        $now_gift_count2 = Db::name('single')
//            ->where('parent_id',$now_gift_count[0]['id'])
//            ->select()->toArray();
        $now_gift_count2 = Db::name('order_gift_count')
            ->alias('ogc')
            ->join('single s','ogc.single_content = s.id')
            ->where('order_id',$order_id)
            ->where('ogc.gift_count','gt',0)
            ->where('single_id',$now_gift_count[0]['id'])
            ->select()->toArray();
        ;//剩余次数大于0的礼包记录数 //一级类
           ;//剩余次数大于0的礼包记录数 //一级类
        if(!empty($now_gift_count2)){
            foreach ($now_gift_count2 as $k => &$v){ //默认第一个门店的服务类人员金额
                $person_price = Db::name('shop_service_price')->where('shop_id',$shop_id)->where('single_id',$v['id'])->value('person_price');
                if(is_null($person_price)) $person_price = '0.00';
                $v['person_price'] =  $person_price;
            }
        }
        return ['one_level' => $now_gift_count,'two_level' => $now_gift_count2];

    }


}